package gpl.scotlandyard.ui.dialogs;

import gpl.scotlandyard.beans.basics.Player;
import gpl.scotlandyard.utils.Config;
import gpl.scotlandyard.utils.I18n;
import gpl.scotlandyard.utils.ImageProvider;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JDialog;

import org.apache.log4j.Logger;

/**
 * Show special moves available for player. Doesn't support dynamic updates,
 * must be setVisible(false) and then setVisible(true).
 * 
 * @author Norbert Martin
 */
public class SpecialDialog {

  private static final Logger _log = Logger.getLogger(SpecialDialog.class);
  private final JDialog jdialog = new JDialog();
  private final Player player;
  private boolean isInit;
  private final JCheckBox hiddenCheck = new JCheckBox(new AbstractAction(I18n.get("HIDDEN_MOVE")) {
    private static final long serialVersionUID = 1L;

    @Override
    public void actionPerformed(ActionEvent e) {
      player.setNextMoveHidden(hiddenCheck.isSelected());
      if (hiddenCheck.isSelected() && player.getHide() < 2) {
        doubleCheck.setEnabled(false);
        doubleCheck.setSelected(false);
        player.setDoDoubleMove(false);
      } else {
        doubleCheck.setEnabled(true);
      }
    }
  });
  private final JCheckBox doubleCheck = new JCheckBox(new AbstractAction(I18n.get("DOUBLE_MOVE")) {
    private static final long serialVersionUID = 1L;

    @Override
    public void actionPerformed(ActionEvent e) {
      player.setDoDoubleMove(doubleCheck.isSelected());
    }
  });

  /**
   * Creates a special dialog for this player.
   * 
   * @param p
   */
  public SpecialDialog(Player p) {
    p.getClass();
    player = p;
  }

  /**
   * Sets visible the dialog. Updates content if is set to true.
   * 
   * @param b
   */
  public void setVisible(boolean b) {
    if (b && !isInit) {
      init();
    }
    if (b) {
      update();
      _log.info("special view showed");
    } else {
      _log.info("special view hidden");
    }
    jdialog.setVisible(b);
  }

  /**
   * Initializes the dialog.
   */
  private void init() {
    jdialog.setIconImage(ImageProvider.get(Config.get("SPECIAL_ICO")).getImage());
    jdialog.setLocationByPlatform(true);
    jdialog.setAlwaysOnTop(true);
    jdialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    jdialog.setResizable(false);
    jdialog.setTitle(String.format(I18n.get("SPECIAL_MOVES"), player.getName()));
    update();
    jdialog.getContentPane().setLayout(new FlowLayout());
    jdialog.getContentPane().add(hiddenCheck);
    jdialog.getContentPane().add(doubleCheck);
    jdialog.pack();
  }

  /**
   * Updates the dialog content.
   */
  private void update() {
    hiddenCheck.setSelected(player.isNextMoveHidden());
    hiddenCheck.setEnabled(!player.isNextMoveHidden() && !player.isProceedDoubleMove());
    hiddenCheck.setVisible(player.getHide() > 0);
    doubleCheck.setSelected(false);
    doubleCheck.setVisible(player.getTwice() > 0);
    doubleCheck.setEnabled(!player.isProceedDoubleMove());
  }
}
